package com.welink.cmp.meatdata.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.welink.cmp.convertor.GenericConvertor;
import com.welink.cmp.dao.metadata.dataobject.EipDO;
import com.welink.cmp.dao.metadata.dataobject.FirewallDO;
import com.welink.cmp.dto.metadata.eip.CreateEipAddCmd;
import com.welink.cmp.dto.metadata.eip.SetupEipUpdateCmd;
import com.welink.cmp.dto.metadata.eip.clientobject.EipCO;
import com.welink.cmp.dto.metadata.eip.query.EipListByPageQry;
import com.welink.cmp.meatdata.command.EipCmdExe;
import com.welink.common.dto.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * EIP应用服务类
 * 提供EIP相关的应用服务接口
 */
@Service
public class EipAppService {

    @Autowired
    private EipCmdExe eipCmdExe;

    /**
     * 添加EIP
     *
     * @param addCmd 添加EIP的命令对象
     * @return 新增EIP的ID
     */
    public Long addEip(CreateEipAddCmd addCmd) {
        final EipDO eipDO = eipCmdExe.checkExistByName(addCmd.getName());
        if (eipDO != null){
            return eipDO.getId();
        }
        return eipCmdExe.save(addCmd);
    }

    /**
     * 更新EIP
     *
     * @param updCmd 更新EIP的命令对象
     * @return 更新是否成功
     */
    public Boolean updateEip(SetupEipUpdateCmd updCmd) {
        return eipCmdExe.update(updCmd);
    }

    /**
     * 根据ID获取EIP
     *
     * @param id EIP ID
     * @return EIP业务对象
     */
    public EipCO getEipById(Long id) {
        return eipCmdExe.queryById(id);
    }

    /**
     * 分页查询EIP
     *
     * @param pageQry 分页查询条件
     * @return 分页响应，包括EIP列表和分页信息
     */
    public PageResponse<EipCO> eipPageQuery(EipListByPageQry pageQry) {
        final Page<EipDO> pageR = eipCmdExe.pageQuery(pageQry);
        List<EipDO> records = pageR.getRecords();
        List<EipCO> coList = GenericConvertor.toCOList(records, EipCO.class);
        return PageResponse.of(coList,pageR.getTotal(),pageR.getSize(),pageR.getCurrent());
    }
}